#import "2DArray.h"

@interface 2DArray(2DArrayPrivate) {

}

@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation 2DArray

@synthesize array;
@synthesize rowsCount;
@synthesize columnCount;

- (id)initWithRowsCount:(NSInteger)aRowsCount columnCount:(NSInteger)aColumnCount {
    
    self = [super init];
    
    if (self != nil) {
        
        NSMutableArray *columnArray = [NSMutableArray arrayWithCapacity:aColumnCount];
        for(int i = 0; i < aColumnCount; i++) {
            [columnArray addObject:@""];
        }

        array = [[NSMutableArray alloc] initWithCapacity:aRowsCount];
        for(int i = 0; i < aRowsCount; i++) {
            [array addObject: columnArray];
        }        
        
        self.rowsCount = aRowsCount;
        self.columnCount = aColumnCount;
    }
    
    return self;
}

- (void)insertObject:(id)anObject withRow:(NSInteger)aRow column:(NSInteger)aColumn {
    
    NSParameterAssert(anObject);
    NSParameterAssert(aRow <= self.rowsCount || aColumn <= self.columnCount);

    NSMutableArray *rowArray = [self.array objectAtIndex:aRow];
    [rowArray replaceObjectAtIndex:aColumn withObject:anObject];
}

- (id)objectAtRow:(NSInteger)aRow column:(NSInteger)aColumn {

    NSParameterAssert(aRow <= self.rowsCount || aColumn <= self.columnCount);
    return [[self.array objectAtIndex:aRow] objectAtIndex:aColumn];
}

- (void)dealloc {
    self.array = nil;
    [super dealloc];
}

@end
